*     *****************************************************************   *;
*     *****************************************************************   *; 
*   Date:          December 27, 2021                               	      *;
*   Authors:       Lisa Dellmuth and Jonas Tallberg				          *;  
*   Purpose: 	   Analyses for book Legitimacy Politics: Elite  		  *;
*    Communication and Public Opinion in Global Governance (CUP, 2023)    *;
*     ****************************************************************    *;
*     ****************************************************************    *;

version 14.2

set more off

use Elitecom.dta, clear

*     ****************************************************************  *;
*     Appendix for Chapter 5									        *;
*     ****************************************************************  *;


*     ****************************************************************  *;
*     Replicate Appendix K										        *;
*     ****************************************************************  *;
		  


use partycues_us.dta, clear

tab partisanid
tab partisanstr
tab Q7
gen no=1
graph bar (percent) no [aweight=weight], over(Q7)

use partycues_de.dta, clear

tab partisanid
tab partisanstr
tab Q7
gen no=1
graph bar (percent) no [aweight=weight], over(Q7)

*Figure 1:
graph combine partisans_de.gph partisans_us.gph, ycommon

*Figure 2:
graph bar partisanstr, over(Q7) stack

tab Q8
contract partisanstr Q7, nomiss
bys Q7 (partisanstr) : replace _freq = sum(_freq)
bys Q7 (partisanstr) : replace _freq = _freq/_freq[_N]*100

twoway bar _freq Q7 if partisanstr == 3 , barw(.5) || ///
       bar _freq Q7 if partisanstr == 2 , barw(.5) || ///
       bar _freq Q7 if partisanstr == 1 , barw(.5) || ///
       bar _freq Q7 if partisanstr == 0 , barw(.5)    ///
       legend(order(4 "Very close"                    ///
                    3 "Quite close"                         ///
                    2 "Not very close"                      /// 
                    1 "Not close at all"))                       ///
       ytitle(Percent of partisans in Germany)                     ///
       xlab(1/9, val)
	   
	 
*Figure 3:
use partycues_us.dta, clear
by Q7, sort:tab Q8 
tab Q8, nol

contract partisanstr Q7, nomiss
bys Q7 (partisanstr) : replace _freq = sum(_freq)
bys Q7 (partisanstr) : replace _freq = _freq/_freq[_N]*100

twoway bar _freq Q7 if partisanstr == 3 , barw(.5) || ///
       bar _freq Q7 if partisanstr == 2 , barw(.5) || ///
       bar _freq Q7 if partisanstr == 1 , barw(.5) || ///
       bar _freq Q7 if partisanstr == 0 , barw(.5)    ///
       legend(order(4 "Very close"                    ///
                    3 "Quite close"                         ///
                    2 "Not very close"                      /// 
                    1 "Not close at all"))                       ///
       ytitle(Percent of partisans in the US)                     ///
       xlab(1/2, val)
	 
	
	
	
*     ****************************************************************  *;
*     Replicate Appendix L										        *;
*     ****************************************************************  *;
		
	
use partycues_us.dta, clear

*Graph only for Democrats, where interactions are significant

tab partystr

svy:reg confnato t2_dem#partystr
eststo m1
svy:reg confnato t3_dem#partystr
eststo m2
svy:reg confnato t4_dem#partystr  
eststo m3
svy:reg confuno t2_dem#partystr
eststo m4
svy:reg confuno t3_dem#partystr
eststo m5
svy:reg confuno t4_dem#partystr  
eststo m6
#delimit ;
esttab m1 m2 m3 m4 m5 m6 using regression_H2_dem_partystr_us.rtf,
starlevels(* .05 ** .01 *** .001) b(%9.3f) r2(%9.3f)  ar2(%9.3f) nodepvars 
se(%9.3f) replace;
#delimit cr


svy:reg confnato t2_rep#partystr
eststo m1
svy:reg confnato t3_rep#partystr
eststo m2
svy:reg confnato t4_rep#partystr  
eststo m3
svy:reg confuno t2_rep#partystr
eststo m4
svy:reg confuno t3_rep#partystr
eststo m5
svy:reg confuno t4_rep#partystr  
eststo m6
#delimit ;
esttab m1 m2 m3 m4 m5 m6 using regression_H2_rep_partystr_us.rtf,
starlevels(* .05 ** .01 *** .001) b(%9.3f) r2(%9.3f)  ar2(%9.3f) nodepvars 
se(%9.3f) replace;
#delimit cr



use partycues_de.dta, clear

tab partystr

svy:reg confnato t2_spd#partystr
eststo m1
svy:reg confnato t3_spd#partystr
eststo m2
svy:reg confnato t4_spd#partystr  
eststo m3
svy:reg confuno t2_spd#partystr
eststo m4
svy:reg confuno t3_spd#partystr
eststo m5
svy:reg confuno t4_spd#partystr  
eststo m6

#delimit ;
esttab m1 m2 m3 m4 m5 m6 using regression_H2_spd_partystr_us.rtf,
starlevels(* .05 ** .01 *** .001) b(%9.3f) r2(%9.3f)  ar2(%9.3f) nodepvars 
se(%9.3f) replace;
#delimit cr


svy:reg confnato t2_cdu#partystr
eststo m1
svy:reg confnato t3_cdu#partystr
eststo m2
svy:reg confnato t4_cdu#partystr  
eststo m3
svy:reg confuno t2_cdu#partystr
eststo m4
svy:reg confuno t3_cdu#partystr
eststo m5
svy:reg confuno t4_cdu#partystr  
eststo m6
#delimit ;
esttab m1 m2 m3 m4 m5 m6 using regression_H2_cdu_partystr_us.rtf,
starlevels(* .05 ** .01 *** .001) b(%9.3f) r2(%9.3f)  ar2(%9.3f) nodepvars 
se(%9.3f) replace;
#delimit cr



*     ****************************************************************  *;
*     Replicate Appendix M										        *;
*     ****************************************************************  *;
	

*Balance tests 

use partycues_us.dta, clear

ttest educ , by(t2) 
ttest educ , by(t3) 
ttest educ , by(t4)

ttest Age , by(t2) 
ttest Age , by(t3) 
ttest Age , by(t4) 

ttest gender , by(t2) 
ttest gender , by(t3) 
ttest gender , by(t4) 

ttest lr , by(t2) 
ttest lr , by(t3) 
ttest lr , by(t4)  

ttest trust , by(t2) 
ttest trust , by(t3) 
ttest trust , by(t4) 

ttest media , by(t2) 
ttest media , by(t3) 
ttest media , by(t4) 

ttest govc , by(t2) 
ttest govc , by(t3) 
ttest govc , by(t4) 

ttest discuss , by(t2) 
ttest discuss , by(t3) 
ttest discuss , by(t4) 


use partycues_de.dta, clear


ttest educ , by(t2) 
ttest educ , by(t3) 
ttest educ , by(t4)

ttest age , by(t2) 
ttest age , by(t3) 
ttest age , by(t4) 

ttest gender , by(t2) 
ttest gender , by(t3) 
ttest gender , by(t4) 

ttest lr , by(t2) 
ttest lr , by(t3) 
ttest lr , by(t4)  

ttest trust , by(t2) 
ttest trust , by(t3) 
ttest trust , by(t4) 

ttest media , by(t2) 
ttest media , by(t3) 
ttest media , by(t4) 

ttest govc , by(t2) 
ttest govc , by(t3) 
ttest govc , by(t4) 

ttest discuss , by(t2) 
ttest discuss , by(t3) 
ttest discuss , by(t4) 




*Analysis using those who passed manipulation checks
***************************

*US

use partycues_us.dta, clear

tab Q12 //Q12a correct
tab Q12, nol //Q12a correct
tab Q13, nol //Q13b correct
tab Q16, nol //Q16a correct
tab Q17 //Q17b correct

gen passedn=.
replace passedn=0
replace passedn=1 if Q12==1|Q13==2
tab passedn
gen passedu=0
replace passedu=1 if Q16==1|Q17==2 
tab passedu


tab Q7
tab t1_dem //compares dem partisan in T1 to dem partisans in CG
tab t1_rep // //compares rep partisan in T1 to rep partisans in CG

reg confnato t2_dem#passedn [aw=weight] 
eststo m1
reg confnato t3_dem#passedn [aw=weight] 
eststo m2
reg confnato t4_dem#passedn [aw=weight] 
eststo m3
reg confuno t2_dem#passedu [aw=weight] 
eststo m4
reg confuno t3_dem#passedu [aw=weight] 
eststo m5
reg confuno t4_dem#passedu [aw=weight] 
eststo m6

reg confnato t2_rep#passedn [aw=weight] 
eststo m7
reg confnato t3_rep#passedn [aw=weight] 
eststo m8
reg confnato t4_rep#passedn [aw=weight] 
eststo m9
reg confuno t2_rep#passedu [aw=weight] 
eststo m10
reg confuno t3_rep#passedu [aw=weight] 
eststo m11
reg confuno t4_rep#passedu [aw=weight] 
eststo m12

#delimit ;
esttab m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 using regression_H1_us_manipulationchecks.rtf,
starlevels(* .05 ** .01 *** .001) b(%9.3f) r2(%9.3f)  ar2(%9.3f) nodepvars 
se(%9.3f) replace;
#delimit cr


*Germany

use partycues_de.dta, clear

tab Q12 //Q12a correct
tab Q12, nol //Q12a correct
tab Q13, nol //Q13b correct
tab Q16, nol //Q16a correct
tab Q17 //Q17b correct

gen passedn=.
replace passedn=0
replace passedn=1 if Q12==1|Q13==2
tab passedn
gen passedu=0
replace passedu=1 if Q16==1|Q17==2 
tab passedu

reg confnato t2_spd#passedn [aw=weight] 
eststo m1
reg confnato t3_spd#passedn [aw=weight] 
eststo m2
reg confnato t4_spd#passedn [aw=weight] 
eststo m3
reg confuno t2_spd#passedu [aw=weight] 
eststo m4
reg confuno t3_spd#passedu [aw=weight] 
eststo m5
reg confuno t4_spd#passedu [aw=weight] 
eststo m6

reg confnato t2_cdu#passedn [aw=weight] 
eststo m7
reg confnato t3_cdu#passedn [aw=weight] 
eststo m8
reg confnato t4_cdu#passedn [aw=weight] 
eststo m9
reg confuno t2_cdu#passedu [aw=weight] 
eststo m10
reg confuno t3_cdu#passedu [aw=weight] 
eststo m11
reg confuno t4_cdu#passedu [aw=weight] 
eststo m12

#delimit ;
esttab m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 using regression_H1_de_manipulationchecks.rtf,
starlevels(* .05 ** .01 *** .001) b(%9.3f) r2(%9.3f)  ar2(%9.3f) nodepvars 
se(%9.3f) replace;
#delimit cr



*Analysis using interactions with pre-treatment beliefs
******************************************************

/*use:
govc
nato_effdem
un_effdem
educ
know
*/

*US

use partycues_us.dta, clear

tab Q9a
tab Q9a, nol
recode Q9a (977=.), gen(un_effdem)
recode Q9b (977=.), gen(nato_effdem)

tab Q10a
tab Q10a, nol
recode Q10a (977=.), gen(defense_approve)
recode Q10b (977=.), gen(refugees_approve)

tab Q4a 
tab Q4b 
tab Q4c
rename Q4a unsc
rename Q4b imf
rename Q4c amnesty

recode unsc 3=1 1/2=0 977=0 //DK coded as incorrect
recode imf 2/977=0 //DK coded as incorrect
recode amnesty 2=1 1=0 3=0 977=0 //DK coded as incorrect

gen know_correct_index=unsc+amnesty
tab know_correct_index


tab Q7
tab t1_dem //compares dem partisan in T1 to dem partisans in CG
tab t1_rep // //compares rep partisan in T1 to rep partisans in CG

*



* ATE dependent on pre-treatment opinions? - US - get predicted probabilities
***********************************************
gen nato_effdem_d=.
replace nato_effdem_d=0 if nato_effdem<6
replace nato_effdem_d=1 if nato_effdem>=6

gen un_effdem_d=.
replace un_effdem_d=0 if un_effdem<6
replace un_effdem_d=1 if un_effdem>=6



reg confnato i.t1_dem##i.nato_effdem_d [aw=weight] 
margins t1_dem, at(nato_effdem_d=(0 1)) vsquish  
marginsplot, xtitle(Pre-treatment legitimacy beliefs in NATO) plot2opts(lpattern("--")) ///
xlabel(`=0' "Strongly disagree" `=1' "Strongly agree", labsize(vsmall)) xscale(range(0 1)) ///
title(Treatment group 1 for Democrats) legend(off) ///
saving(m1_nato_effdem.gph, replace) 

reg confnato i.t2_dem##i.nato_effdem_d [aw=weight] 
margins t2_dem, at( nato_effdem_d=(0 1)) vsquish  
marginsplot, xtitle(Pre-treatment legitimacy beliefs in NATO) plot2opts(lpattern("--")) ///
xlabel(`=0' "Strongly disagree" `=1' "Strongly agree", labsize(vsmall)) xscale(range(0 1)) ///
title(Treatment group 2 for Democrats) legend(off) ///
saving(m2_nato_effdem.gph, replace) 

reg confnato i.t3_dem##i.nato_effdem_d [aw=weight] 
margins t3_dem, at( nato_effdem_d=(0 1)) vsquish  
marginsplot, xtitle(Pre-treatment legitimacy beliefs in NATO) plot2opts(lpattern("--")) ///
xlabel(`=0' "Strongly disagree" `=1' "Strongly agree", labsize(vsmall)) xscale(range(0 1)) ///
title(Treatment group 3 for Democrats) legend(off) ///
saving(m3_nato_effdem.gph, replace) 


reg confuno i.t1_dem##i.un_effdem_d [aw=weight] 
margins t1_dem, at( un_effdem_d=(0 1)) vsquish  
marginsplot, xtitle(Pre-treatment legitimacy beliefs in the UN) plot2opts(lpattern("--")) ///
xlabel(`=0' "Strongly disagree" `=1' "Strongly agree", labsize(vsmall)) xscale(range(0 1)) ///
title(Treatment group 1 for Democrats) legend(off) ///
saving(m1_un_effdem.gph, replace) 

reg confuno i.t2_dem##i.un_effdem_d [aw=weight] 
margins t2_dem, at( un_effdem_d=(0 1)) vsquish  
marginsplot, xtitle(Pre-treatment legitimacy beliefs in the UN) plot2opts(lpattern("--")) ///
xlabel(`=0' "Strongly disagree" `=1' "Strongly agree", labsize(vsmall)) xscale(range(0 1)) ///
title(Treatment group 2 for Democrats) legend(off) ///
saving(m2_un_effdem.gph, replace) 

reg confuno i.t3_dem##i.un_effdem_d [aw=weight] 
margins t3_dem, at( un_effdem_d=(0 1)) vsquish  
marginsplot, xtitle(Pre-treatment legitimacy beliefs in the UN) plot2opts(lpattern("--")) ///
xlabel(`=0' "Strongly disagree" `=1' "Strongly agree", labsize(vsmall)) xscale(range(0 1)) ///
title(Treatment group 3 for Democrats) legend(off) ///
saving(m3_un_effdem.gph, replace) 

graph combine m1_nato_effdem.gph m2_nato_effdem.gph m3_nato_effdem.gph  ///
m1_un_effdem.gph m2_un_effdem.gph m3_un_effdem.gph, ycommon


* get differences in treatment effects across categories

*reg confuno i.t1_dem##i.un_effdem_d [aw=weight] 
*eststo test
*coefplot(test)

reg confnato i.t1_dem##i.nato_effdem_d [aw=weight] 
margins nato_effdem_d, dydx(t1_dem) vsquish 
marginsplot, saving(m1_nato_effdem_d.gph, replace) 
reg confnato i.t2_dem##i.nato_effdem_d [aw=weight] 
margins nato_effdem_d, dydx(t2_dem) vsquish 
marginsplot, saving(m2_nato_effdem_d.gph, replace) 
reg confnato i.t3_dem##i.nato_effdem_d [aw=weight] 
margins nato_effdem_d, dydx(t3_dem) vsquish 
marginsplot, saving(m3_nato_effdem_d.gph, replace) 

reg confuno i.t1_dem##i.un_effdem_d [aw=weight] 
margins un_effdem_d, dydx(t1_dem) vsquish 
marginsplot, saving(m1_un_effdem_d.gph, replace) 
reg confuno i.t2_dem##i.un_effdem_d [aw=weight] 
margins un_effdem_d, dydx(t2_dem) vsquish 
marginsplot, saving(m2_un_effdem_d.gph, replace) 
reg confuno i.t3_dem##i.un_effdem_d [aw=weight] 
margins un_effdem_d, dydx(t3_dem) vsquish 
marginsplot, saving(m3_un_effdem_d.gph, replace) 

*significant for NATO throughout, but never for UN

graph combine m1_nato_effdem_d.gph m2_nato_effdem_d.gph m3_nato_effdem_d.gph  ///
m1_un_effdem_d.gph m2_un_effdem_d.gph m3_un_effdem_d.gph, ycommon



 



* ATE dependent on pre-treatment opinions? - DE
***********************************************
gen nato_effdem_d=.
replace nato_effdem_d=0 if nato_effdem<6
replace nato_effdem_d=1 if nato_effdem>=6

gen un_effdem_d=.
replace un_effdem_d=0 if un_effdem<6
replace un_effdem_d=1 if un_effdem>=6

 

reg confnato t2_cdu if nato_effdem_d==1 [aw=weight] 


reg confnato i.t2_cdu##i.nato_effdem_d [aw=weight] 
margins t2_cdu, at( nato_effdem_d=(0 1)) vsquish  
marginsplot, xtitle(Pre-treatment legitimacy beliefs in NATO) plot2opts(lpattern("--")) ///
xlabel(`=0' "Strongly disagree" `=1' "Strongly agree", labsize(vsmall)) xscale(range(0 1)) ///
title(Treatment group 2 for CDU/CSU) legend(off) ///
saving(m2_nato_effdem.gph, replace) 

reg confuno i.t2_cdu##i.un_effdem_d [aw=weight] 
margins t2_cdu, at( un_effdem_d=(0 1)) vsquish  
marginsplot, xtitle(Pre-treatment legitimacy beliefs in the UN) plot2opts(lpattern("--")) ///
xlabel(`=0' "Strongly disagree" `=1' "Strongly agree", labsize(vsmall)) xscale(range(0 1)) ///
title(Treatment group 2 for CDU/CSU) legend(off) ///
saving(m2_un_effdem.gph, replace) 

reg confuno i.t3_cdu##i.un_effdem_d [aw=weight] 
margins t3_cdu, at( un_effdem_d=(0 1)) vsquish  
marginsplot, xtitle(Pre-treatment legitimacy beliefs in the UN) plot2opts(lpattern("--")) ///
xlabel(`=0' "Strongly disagree" `=1' "Strongly agree", labsize(vsmall)) xscale(range(0 1)) ///
title(Treatment group 3 for CDU/CSU) legend(off) ///
saving(m3_un_effdem.gph, replace) 

graph combine m2_nato_effdem.gph m2_un_effdem.gph m3_un_effdem.gph, ycommon col(3)


reg confnato i.t1_cdu##i.nato_effdem_d [aw=weight] 
reg confnato i.t1_spd##i.nato_effdem_d [aw=weight] 
margins nato_effdem_d, dydx(t1_cdu) vsquish 
marginsplot, saving(m1_nato_effdem_d.gph, replace) 

reg confnato i.t2_cdu##i.nato_effdem_d [aw=weight] 
margins nato_effdem_d, dydx(t2_cdu) vsquish 
marginsplot, saving(m2_nato_effdem_d.gph, replace) 

reg confuno i.t2_cdu##i.un_effdem_d [aw=weight] 
margins un_effdem_d, dydx(t2_cdu) vsquish 
marginsplot, saving(m2_un_effdem_d.gph, replace) 

reg confuno i.t3_cdu##i.un_effdem_d [aw=weight] 
margins un_effdem_d, dydx(t3_cdu) vsquish 
marginsplot, saving(m3_un_effdem_d.gph, replace) 

graph combine m2_nato_effdem_d.gph m2_un_effdem_d.gph m3_un_effdem_d.gph, ycommon col(3)
